package com.lagou.controller;

import com.lagou.pojo.Article;
import com.lagou.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.JpaSort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.awt.print.Pageable;
import java.util.List;

@RequestMapping("/blog")
@Controller
public class BlogController {

    @Autowired
    private IArticleService articleService ;


    @RequestMapping("/index")
    public String index (Model model, Integer page, Boolean isEnd) {
        page = page == null ? 1:
                page - 1 >= 0 ? page - 1
                : page;
        isEnd = isEnd == null ? false: isEnd;
        model.addAttribute("title", "blog");
        model.addAttribute("keyword", "blog");
//        List<Article> all = articleService.findAll();
        Integer limit = 3;
        PageRequest pageRequest = PageRequest.of(page, limit);
        Page<Article> all = articleService.findAll(pageRequest);
        if(isEnd) {
            page = all.getTotalPages();
            all = articleService.findAll(pageRequest);
        }
        model.addAttribute("ariticles", all);
        model.addAttribute("page", page + 1);
        model.addAttribute("totalPage", all.getTotalPages());
        return "index";
    }
}
